2025-05-29
Custom Emacs qalc REPL Pop-Up
I like to use qalc for calculating things. I know emacs has both
calc
and quick-calc
, but I am very used to qalc
, more
specifically it's CLI version.
I mostly stick to qalc because it understands (custom) units. I am currently working with a binary fileformat which has multiple odd ways to count time. Qalc is very helpful here:
> 1.5625ns to 260.41666ps 1.5625 nanoseconds ≈ 6.000000154 × 260.41666 ps
I could get that number without qalc, but it gets me there just a tiny bit more comfortably.
So for a long time now I've always just opened a terminal running qalc next to emacs. It only recently occured to me that I can run qalc inside emacs!
So first, here is a function which wraps any programm that presents a REPL in an emacs buffer plus window. We are using normal shell mode, so we only have to worry about wrangling buffers.
(defun lhp/replify (cmd bname) (if (string= bname (buffer-name)) (quit-window) (pop-to-buffer (or (get-buffer bname) (window-buffer (async-shell-command cmd bname)))))) (defun qalc () "Open a qalc REPL" (interactive) (lhp/replify "qalc" "*qalc*"))
A REPL function using this helper function will try to focus the window displaying the REPL buffer, optionally creating the buffer, launching the program and creating the window. Is the buffer already focused, instead close the window.
As you can probably tell, I really like dwim
– "Do what I mean" – commands in
emacs. Functions which can do one of multiple different actions, based
on a usually reasonable, heuristics based guess.
Arguably closing the window is a more opinionated choice than hiding the buffer. But it makes sense if I show you the second half of this:
(setq display-buffer-alist '(;; ... ("\*qalc\*" (display-buffer-reuse-mode-window display-buffer-in-side-window) (dedicated . t) (inhibit-switch-frame . t) (side . bottom)) ;; ... ))
Using an entry in display-buffer-alist
, I told emacs to open my
special *qalc*
REPL buffer in a dedicated side-window at the bottom
of the frame. Combined with a keybind for the qalc
function (I use
C-c q
), I have a very quick and convenient way of showing and hiding
a popup with a qalc
REPL.
I literally have to think about nothing: Press C-c q
once to open
the REPL. Press again to hide it. My window layout remains undisturbed
thanks to using a side-window and emacs takes care of spawning the
program if it is not already running. And since the window is closed
but the buffer isn't killed, the REPL session will stay alive;
Basically like a little math scratchpad.
Side-windows are the best way I have found to make emacs window
management pleasant. Everything that comes and goes often and isn't
the main buffer required for a task should probably be a
side-window. For me that mostly means my qalc
REPL (bottom),
compilation (bottom) and rgrep
plus occur
(right).
Warm air and buzzing insects surround me. The kind of day were you can actually work outside. The kind of work that feels good but isn't too productive. The kind of productivity that is more fun than ultimately useful.
Why bring the kitchen sink if you can bring a bath tub?
I decided to share this as a blog post because I think chances are high someone else will find this useful as well. I hope your day is as lovely as mine 🐈